
#include "Base.h"
#include "Game/InputManagerComponent.h"
#include "Game/GameTime.h"
#include "Game/Game.h"
#include "Game/GameComponent.h"
#include <string.h>
#include "Maths/Math.h"

namespace CasaEngine
{

InputManagerComponent::InputManagerComponent(Game* pGame_) :
	IGameComponent(pGame_)		
{
	UpdateOrder(MaxInt);

	m_pInput = pGame_->GetInput();

	m_LastFrameMouseX = 0;
	m_LastFrameMouseY = 0;
	m_LastFrameMouseDeltaX = 0;
	m_LastFrameMouseDeltaY = 0;
	m_LastFrameMouseWheel = 0;

	memset(m_LastFrameKeys, 0, sizeof(bool) * Key::Count);
	memset(m_LastFrameMouseButtons, 0, sizeof(bool) * Mouse::ButtonCount);
	memset(m_LastFrameJoystickButtons[0], 0, sizeof(bool) * Joy::Count * Joy::ButtonCount);
	memset(m_LastFrameJoystickAxis[0], 0, sizeof(float) * Joy::Count * Joy::AxisCount);
}


InputManagerComponent::~InputManagerComponent()
{
}

void InputManagerComponent::Initialize()
{
	
}

void InputManagerComponent::Update(const GameTime& /*gameTime_*/)
{
	m_pInput->CopyAllSate(m_LastFrameKeys, m_LastFrameMouseButtons, 
		m_LastFrameMouseX, m_LastFrameMouseY, m_LastFrameMouseWheel,
		m_LastFrameMouseDeltaX, m_LastFrameMouseDeltaY,
		m_LastFrameJoystickButtons[0], m_LastFrameJoystickAxis[0]);

	m_LastFrameMouseDeltaX = m_pInput->GetMouseDeltaX();
	m_LastFrameMouseDeltaY = m_pInput->GetMouseDeltaY();
	m_LastFrameMouseWheel = m_pInput->GetMouseWheel();

	m_pInput->ResetDelta();
}

/**
 * 
 */
bool InputManagerComponent::IsKeyDown( Key::Code KeyCode ) const
{
	return m_pInput->IsKeyDown(KeyCode);
}

/**
 * 
 */
bool InputManagerComponent::IsKeyJustDown( Key::Code KeyCode ) const
{
	return m_pInput->IsKeyDown(KeyCode)
		&& m_LastFrameKeys[KeyCode] == false;
}

/**
 * 
 */
bool InputManagerComponent::IsMouseButtonDown( Mouse::Button Button ) const
{
	return m_pInput->IsMouseButtonDown(Button);
}

/**
 * 
 */
bool InputManagerComponent::IsMouseButtonJustDown( Mouse::Button Button ) const
{
	return m_pInput->IsMouseButtonDown(Button)
		&& m_LastFrameMouseButtons[Button] == false;
}

/**
 * 
 */
bool InputManagerComponent::IsJoystickButtonDown( unsigned int JoyId, unsigned int Button ) const
{
	return m_pInput->IsJoystickButtonDown(JoyId, Button);
}

/**
 * 
 */
bool InputManagerComponent::IsJoystickButtonJustDown( unsigned int JoyId, unsigned int Button ) const
{
	return m_pInput->IsJoystickButtonDown(JoyId, Button)
		&& m_LastFrameJoystickButtons[Button] == false;
}

/**
 * 
 */
int InputManagerComponent::GetDeltaMouseX() const
{
	return m_LastFrameMouseDeltaX;
}

/**
 * 
 */
int InputManagerComponent::GetDeltaMouseY() const
{
	return m_LastFrameMouseDeltaY;
}

/**
 * 
 */
int InputManagerComponent::GetMouseX() const
{
	return m_pInput->GetMouseX();
}

/**
 * 
 */
int InputManagerComponent::GetMouseY() const
{
	return m_pInput->GetMouseY();
}

/**
 * 
 */
int InputManagerComponent::GetMouseWheel() const
{
	return m_LastFrameMouseWheel;
}

/**
 * 
 */
float InputManagerComponent::GetJoystickAxis( unsigned int JoyId, Joy::Axis Axis ) const
{
	return m_pInput->GetJoystickAxis(JoyId, Axis);
}

} // namespace CasaEngine
